﻿using System;
using System.Globalization;
using System.Windows.Data;

namespace Xcj.Mes.WholeLine.ZH.HG.Shell.Converter
{
    public class BitConverter : IValueConverter
    {
        // Convert: 源数据 -> 显示数据 (int -> string)
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                int v = (int)value;

                switch (v)
                {
                    case 0: return "0";
                    case 1: return "1";
                    case 2: return "2";
                    case 3: return "3";
                    case 4: return "4";
                    case 5: return "5";
                    case 6: return "6";
                    case 7: return "7";
                    case 8: return "8";
                    case 9: return "9";
                    case 10: return "A";
                    case 11: return "B";
                    case 12: return "C";
                    case 13: return "D";
                    case 14: return "E";
                    case 15: return "F";
                    default: return "0";
                }
            }
            catch { }

            return "0";
        }

        // ConvertBack: 显示数据 -> 源数据 (string -> int)
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                string v = (string)value;

                switch (v)
                {
                    case "0": return 0;
                    case "1": return 1;
                    case "2": return 2;
                    case "3": return 3;
                    case "4": return 4;
                    case "5": return 5;
                    case "6": return 6;
                    case "7": return 7;
                    case "8": return 8;
                    case "9": return 9;
                    case "A": return 10;
                    case "B": return 11;
                    case "C": return 12;
                    case "D": return 13;
                    case "E": return 14;
                    case "F": return 15;
                    default: return 0;
                }
            }
            catch { }

            return 0;
        }
    }
}
